package com.eurasia.assignment.infrastructure.entity;

import java.time.LocalDateTime;

import com.eurasia.assignment.domain.Submission;
import com.eurasia.assignment.domain.SubmissionStatus;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Data;

@Entity
@Table(name = "submissions")
@Data
public class SubmissionEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "project_id", nullable = false)
    private Long projectId;
    
    private String description;
    
    @Column(nullable = false)
    private String submitterId;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "file_id", nullable = false)
    private FileEntity file;
    
    @Column(nullable = false)
    private LocalDateTime submissionTime;
    
    private String reviewResult;

    @Enumerated(EnumType.STRING)
    private SubmissionStatus reviewStatus;
    
    private String type;
    
    private Long reviewerId;
    
    private LocalDateTime reviewTime;
    
    /**
     * 从领域模型创建实体
     */
    public static SubmissionEntity fromDomain(Submission domain) {
        SubmissionEntity entity = new SubmissionEntity();
        if (domain.getId() != null) {
            entity.setId(Long.valueOf(domain.getId()));
        }
        entity.setProjectId(domain.getProjectId());
        entity.setDescription(domain.getDescription());
        entity.setSubmitterId(domain.getSubmitterId());
        entity.setFile(domain.getFile());
        entity.setSubmissionTime(domain.getSubmissionTime());
        entity.setReviewResult(domain.getReviewResult());
        entity.setReviewStatus(domain.getReviewStatus());
        entity.setType(domain.getType());
        entity.setReviewerId(domain.getReviewerId());
        entity.setReviewTime(domain.getReviewTime());
        return entity;
    }
    
    /**
     * 转换为领域模型
     */
    public Submission toDomain() {
        return Submission.builder()
                .id(this.id != null ? this.id.toString() : null)
                .projectId(this.projectId)
                .description(this.description)
                .submitterId(this.submitterId)
                .file(this.file)
                .submissionTime(this.submissionTime)
                .reviewResult(this.reviewResult)
                .reviewStatus(this.reviewStatus)
                .type(this.type)
                .reviewerId(this.reviewerId)
                .reviewTime(this.reviewTime)
                .build();
    }
}